﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using mscook.core;
using mscook.core.domain;
using mscook.core.repositories;
using mscook.mvc.core;
using mscook.core.services;
using System.Linq.Expressions;


namespace mscook.mvc.Areas.weblogin.Controllers
{
    public class SystemUserController : BaseController
    {
        //
        // GET: /weblogin/SystemUser/
        private ILoginUserService iLoginUserService;
        public SystemUserController(ILoginUserService iLoginUserService)
            : base(iLoginUserService)
        {
            this.iLoginUserService = iLoginUserService;
        }

        public ActionResult Index(string nowPageNum, string per = "", bool isVip = false, string email = "", string loginName = "")
        {
            Expression<Func<LoginUser, bool>> select_expression = o => (string.IsNullOrEmpty(loginName) || o.LoginName.Contains(loginName))
             && (string.IsNullOrEmpty(email) || o.Email.Contains(email));

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<LoginUser> systemUserList = iLoginUserService.SelectLimitSystemUser(isVip, pageHelper.Start, pageHelper.PerValue, out total, select_expression).Where(o=>o.IsVip==isVip).ToList();
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);


            ViewData["loginName"] = loginName;
            ViewData["email"] = email;
            ViewData["isVip"] = isVip;
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            InitNavBar();
            return View(systemUserList);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(string id)
        {
            LoginUser loginUser = iLoginUserService.Get(id);
            if (loginUser != null)
            {
                InitNavBar();
                return View(loginUser);
            }
            return RedirectToIndex();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string Id, string IsVip)
        {
            LoginUser loginUser = iLoginUserService.Get(Id);
            if (ModelState.IsValid)
            {
               
                UpdateModel(loginUser);

                iLoginUserService.SaveOrUpdate(loginUser);

                return RedirectToIndex();
            }
            return View();
        }
        public ActionResult Delete(string id)
        {
            iLoginUserService.Delete(id);
            return RedirectToIndex();
        }


    }
}
